package com.cm.res.scanner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ScannerTask implements Runnable {

    private String filePath;
    private int id;

    public ScannerTask(String filePath, int id) {
        this.filePath = filePath;
        this.id = id;
    }

    @Override
    public void run() {
        handleTask();
        ResourceChecker.getInstance().onScanFinish(filePath, getId());
    }

    private void handleTask() {
        if (filePath == null || filePath.length() == 0) return;
        File file = new File(filePath);
        if (!file.exists()) return;

        if (file.isFile()) {
            checkFile(file);
        } else {
            if (!file.isDirectory()) {
                return;
            }
            File[] listFiles = file.listFiles();
            if (listFiles == null || listFiles.length == 0) return;
            for (File path : listFiles) {
                String absolutePath = path.getAbsolutePath();
                if (ResourceChecker.canExclude(absolutePath)) continue;
                ResourceChecker.getInstance().startScan(absolutePath);
            }
        }

    }


    private void checkFile(File file) {
        BufferedReader br = null;
        try {
            String absolutePath = file.getAbsolutePath();
            if (!(absolutePath.endsWith(".java") || absolutePath.endsWith(".xml") || absolutePath.endsWith(".kt"))) {
                return;
            }

            br = new BufferedReader(new FileReader(file));
            String str = null;
            while ((str = br.readLine()) != null) {
                if (str == null) break;
                if (str.length() <= 4) continue;
                String[] resourceType = getResourceType();
                for (int i = 0; i < resourceType.length; i++) {
                    filterResource(absolutePath, str, resourceType[i]);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void filterResource(String absolutePath, String str, String resourceType) {
        Set<String> resourceFiles = ResourceChecker.getInstance().getResourceFiles();
        if (resourceFiles == null) return;

        List<String> resourceFileList = new ArrayList<>();

        synchronized (resourceFiles) {
            resourceFileList.addAll(resourceFiles);
        }

        Set<String> deleteResourceList = new HashSet<>();

        for (String resPath : resourceFileList) {

            File file = new File(resPath);
            String name = file.getName();
            String[] resourceName = name.split("\\.");
            String resName = resourceName[0];

            if (absolutePath.endsWith(".xml")) {
                String resourceStr = "@" + resourceType + "/" + resName;
                if (str.length() >= resourceStr.length() && str.contains(resourceStr)) {
                    deleteResourceList.add(resPath);
                }
            } else {
                String resourceStr = "R." + resourceType + "." + resName;
                if (str.length() >= resourceStr.length() && str.contains(resourceStr)) {
                    deleteResourceList.add(resPath);
                }
            }
        }

        for (String resPath : deleteResourceList) {
            synchronized (resourceFiles) {
                ResourceChecker.getInstance().notifyRemoveResource(resPath);
            }
        }


    }


    public String[] getResourceType() {
        return ResourceChecker.getInstance().getResourceTypes();
    }

    public int getId() {
        return id;
    }
}
